package java;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class Question20 {
    public static void main(String[] args) {
        System.out.println(isValid("([])()"));
    }
    public static boolean isValid(String s) {
        if (s == null || s.length() < 1){
            return true;
        }
        Map<Character,Integer> map = new HashMap<>();
        map.put('(',0);
        map.put(')',1);
        map.put('{',3);
        map.put('}',4);
        map.put('[',6);
        map.put(']',7);
        Stack<Character> stack = new Stack<>();
        for (int i = 0 ; i < s.length() ; i ++){
            if (stack.isEmpty()){
                stack.push(s.charAt(i));
            }else {
                if (map.get(stack.peek()) + 1 != map.get(s.charAt(i))){
                    stack.push(s.charAt(i));
                }else {
                    stack.pop();
                }
            }
        }
        return stack.isEmpty();
    }
}
